package com.shxc.smarttrade.quotation.adapter;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.TextView;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.quotation.model.GetQuotesModel;
import com.shxc.smarttrade.quotation.model.RealTimeQuoteDatas;
import com.shxc.smarttrade.utils.AppUtils;
import com.shxc.smarttrade.utils.Loger;

import java.text.DecimalFormat;
import java.util.List;

/**
 * 行情 - 实时信息
 */

public class QuotationRealtimeAdapter extends RecyclerView.Adapter<QuotationRealtimeAdapter.ViewHolder> {

    private Context mContext;
    private List<GetQuotesModel.DataBean.SymbolsBean> mList;
    private Handler mHandler;
//    private Typeface tf;

    public QuotationRealtimeAdapter(Context context, List<GetQuotesModel.DataBean.SymbolsBean> list, Handler handler) {
        mContext = context;
        mList = list;
        mHandler = handler;
//        AssetManager mgr = mContext.getAssets();
//        tf = Typeface.createFromAsset(mgr, "fonts/fzltxh.ttf");
//        Loger.e("tf",tf.toString());
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_quotation_realtime, parent, false);
        ViewHolder holder = new ViewHolder(inflate);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        final GetQuotesModel.DataBean.SymbolsBean model = mList.get(position);


        model.setPosition(position);

//        holder.symbolcn_textview.setText(model.getSymbolCn());
//        holder.symbolen_textview.setText(model.getSymbolEn());
        holder.symbolcn_textview.setText(model.getSymbolEn());
        holder.symbolen_textview.setText(model.getSymbolCn());

        int digits = model.getDigits();
        if (digits != -1) {
            SpannableString ss = new SpannableString(AppUtils.getDigitsData(model.getBid(), digits));
            RelativeSizeSpan span = new RelativeSizeSpan((float) 1.5);
            int length = ss.length();
            if (digits > 2) {
                ss.setSpan(span, length - 3, length - 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
            } else {
                ss.setSpan(span, length - 4, length - 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
            }
            holder.price_textview.setText(ss);
        }

        DecimalFormat df = new DecimalFormat("######0.00");

        if (model.getIsOpen().equals("0") && model.getDifference() == 0) {
            holder.percent_textview.setText(df.format(model.getGains()) + "%");

//            holder.percent_textview.setTypeface(tf);
//            holder.price_textview.setTypeface(tf);



            if (model.getGains() > 0) {
//                holder.price_textview.setTextColor(mContext.getResources().getColor(R.color.quotation_orange_bg));
//                holder.percent_textview.setTextColor(mContext.getResources().getColor(R.color.quotation_orange_bg));
                holder.price_textview.setTextColor(Color.parseColor("#E24C4F"));
                holder.percent_textview.setTextColor(Color.parseColor("#E24C4F"));
            } else if (model.getGains() == 0) {
                holder.price_textview.setTextColor(mContext.getResources().getColor(R.color.quotation_gray_bg));
                holder.percent_textview.setTextColor(mContext.getResources().getColor(R.color.quotation_gray_bg));
            } else {
//                holder.price_textview.setTextColor(mContext.getResources().getColor(R.color.quotation_green_bg));
//                holder.percent_textview.setTextColor(mContext.getResources().getColor(R.color.quotation_green_bg));
                holder.price_textview.setTextColor(Color.parseColor("#30BF94"));
                holder.percent_textview.setTextColor(Color.parseColor("#30BF94"));
            }
        } else {
            //计算百分比
            double percent = (model.getBid() - model.getYesterdayClosePrice()) / model.getYesterdayClosePrice() * 100;
            holder.percent_textview.setText(df.format(percent) + "%");

            if (model.getBid() > model.getYesterdayClosePrice()) {
                holder.percent_textview.setTextColor(Color.parseColor("#E24C4F"));
                holder.percent_textview.setText("+" + df.format(percent) + "%");
            } else if (model.getBid() == model.getYesterdayClosePrice()) {
                holder.percent_textview.setTextColor(Color.parseColor("#E24C4F"));
            } else {
                holder.percent_textview.setTextColor(Color.parseColor("#30BF94"));
                holder.percent_textview.setText(df.format(percent) + "%");
            }

            if (model.isState()) {
                holder.price_textview.setTextColor(Color.parseColor("#E24C4F"));
            } else {
                holder.price_textview.setTextColor(Color.parseColor("#30BF94"));
            }

            if (model.getDifference() > 0) {
                holder.price_imageview.setImageResource(R.drawable.bg_round_orange);
                doAnimation(holder.price_imageview);
            } else if (model.getDifference() == 0) {
                holder.price_imageview.setVisibility(View.INVISIBLE);
            } else {
                holder.price_imageview.setImageResource(R.drawable.bg_round_green);
                doAnimation(holder.price_imageview);
            }
        }
        holder.line_view.setVisibility(position == 0 ? View.GONE : View.VISIBLE);

        //跳转页面
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Message msg = new Message();
                msg.what = 1;
                msg.obj = model;
                mHandler.sendMessage(msg);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public void setUpdateList(List<RealTimeQuoteDatas.DataBean.QuoteBean> updateList) {
        for (int i = 0; i < mList.size(); i++) {
            GetQuotesModel.DataBean.SymbolsBean m = mList.get(i);
            RealTimeQuoteDatas.DataBean.QuoteBean um = updateList.get(i);
            m.setDifference(um.getBid() - m.getBid());
            m.setAsk(um.getAsk());
            m.setBid(um.getBid());
            m.setMarket(um.getMarket());
            if (m.getDifference() > 0) {
                m.setState(true);
            } else if (m.getDifference() < 0) {
                m.setState(false);
            }
        }
        notifyDataSetChanged();
    }

    public void seList(List<GetQuotesModel.DataBean.SymbolsBean> qrtlist) {
        mList = qrtlist;
        notifyDataSetChanged();
    }

    /**
     * 渐变动画
     */
    private void doAnimation(final View view) {
        final AlphaAnimation alpha = new AlphaAnimation(1, 0);
        alpha.setDuration(500);
        alpha.setFillAfter(true);
        view.startAnimation(alpha);
        alpha.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                view.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                view.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

    }


    public class ViewHolder extends RecyclerView.ViewHolder {

        protected TextView symbolcn_textview;//中文
        protected TextView symbolen_textview;//英文
        protected TextView price_textview;//价格
        protected ImageView price_imageview;//动画蒙版
        protected TextView percent_textview;//百分比
        protected View line_view;//分割线

        public ViewHolder(View itemView) {
            super(itemView);
            symbolcn_textview = (TextView) itemView.findViewById(R.id.symbolcn_textview);
            symbolen_textview = (TextView) itemView.findViewById(R.id.symbolen_textview);
            price_textview = (TextView) itemView.findViewById(R.id.price_textview);
            price_imageview = (ImageView) itemView.findViewById(R.id.price_imageview);
            percent_textview = (TextView) itemView.findViewById(R.id.percent_textview);
            line_view = itemView.findViewById(R.id.line_view);
        }
    }


}
